package sht.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import annotation.Type;
import sht.commons.BeanArray;
import sht.commons.Constant;
import sht.dto.UserDTO;
import sht.model.User;
import sht.service.custom.UserService;

@Controller
@RequestMapping("/userController")
public class UserController {
	
	private final Logger log = Logger.getLogger(UserController.class.getName())  ;
	
	@Autowired
	UserService userService;

	@RequestMapping("/login")
	public String login(String userid, String password, HttpServletRequest request) {
		User user = userService.findUser(userid, password);
		if (userid != null && !"".equals(userid) && user != null) {
			request.getSession().setAttribute(Constant.SESSION_USER, user);
			return "WEB-INF/page/index";
		}
		return "index";
	}

	@RequestMapping("/logout")
	public String logout(HttpServletRequest request) {
		request.getSession().removeAttribute(Constant.SESSION_USER);
		log.info("logout is running...");
		return "index";
	}

	@RequestMapping("/test")
	public String test(@RequestParam("file") MultipartFile file) {
		User user = new User();
		user.setUserid("zhaoxin");
		user.setUsername("赵信");
		user.setPassword("123456");
		userService.addUser(user);

		return "index";
	}

	/**提交list 类型参数
	 * */
	@RequestMapping(value = "/submitData")
	public String submitData(@RequestBody ArrayList<UserDTO> ba) {
		ba.forEach(item -> {
			System.out.println(item);
		});
		log.info("submitData is running... the user has subimted list<T> type data!");
		return "index";
	}
	
	@RequestMapping(value = "/submitData2")
	public String submitData(String username, String password) {
		System.out.println(username);
		return "index";
	}
	
	/**提交list 类型参数
	 * */
	@RequestMapping(value = "/submitData3")
	public String submitData2(@Type(UserDTO.class) BeanArray<UserDTO> list) {
		list.getList().forEach(item -> {
			System.out.println(item);
		});
		log.info("submitData is running... the user has subimted list<T> type data!");
		return "index";
	}
	
	/**
	 * hibernate find test
	 */
	@RequestMapping("/find")
	public String find(String username, String password) {
		User user = userService.findUser(username, password);
		System.out.println(user);
		return "index";
	}
	
	/**
	 * hibernate query list test
	 */
	@RequestMapping("/findUserByQuery")
	public String findUserByQuery(User user) {
		List<User> users = userService.findUserByQuery(user);
		System.out.println(users);
		log.info("findUserByQuery is running... query data...");
		return "index";
	}
}
